﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    CalendarActionsControl.cs
//
// Abstract:
//    
//    Displays the actions page for a calendar rule.
//
//--

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows.Controls;

namespace Microsoft.EnhancedReminders.OutlookAddIn
{
    /// <summary>
    /// Displays the channels list for a calendar rule.
    /// </summary>
    public partial class CalendarActionsControl : UserControl, INotifyPropertyChanged
    {
        private const int AbsoluteTimeIntervalInMinutes = 15; // Time interval for the absolute time drop-down menu.
        private const int LeadTimeIntervalInMinutes = 5; // Time interval for the join-now time drop down.

        private DateTime _absoluteTime;
        private int _leadTime;

        /// <summary>
        /// Constructor.
        /// </summary>
        public CalendarActionsControl()
        {
            InitializeComponent();

            const int MinutesPerDay = 1440;
            const int MaxLeadTimeInMinutes = 30; // Maximum join now lead time.

            // Populate the absolute time combo box...
            
            // First add the "None" option.
            ComboBoxItem item = new ComboBoxItem();
            item.Content = Properties.Resources.None_Option;
            absoluteTimeComboBox.Items.Add(item);

            DateTime time = new DateTime(1, 1, 1, 0, 0, 0);
            for (int i = 0; i < MinutesPerDay / AbsoluteTimeIntervalInMinutes; i++)
            {
                item = new ComboBoxItem();
                item.Content = time.ToString("t", Thread.CurrentThread.CurrentUICulture);
                absoluteTimeComboBox.Items.Add(item);
                time = time.AddMinutes(AbsoluteTimeIntervalInMinutes);
            }

            // Populate the lead time combo box...

            // Add the "None" option.
            item = new ComboBoxItem();
            item.Content = Properties.Resources.None_Option;
            leadTimeComboBox.Items.Add(item);

            for (int leadTime = 0; leadTime <= MaxLeadTimeInMinutes; leadTime += LeadTimeIntervalInMinutes)
            {
                item = new ComboBoxItem();
                item.Content = String.Format(Thread.CurrentThread.CurrentUICulture, "{0} {1}", leadTime, Properties.Resources.Minutes);
                leadTimeComboBox.Items.Add(item);
            }
        }

        /// <summary>
        /// Initializes the control with the channels lists and time info.
        /// </summary>
        /// <param name="notificationChannels">The list of notification channels.</param>
        /// <param name="joinNowChannels">The list of join now channels.</param>
        public void Initialize(NotificationChannelListAbsoluteTime notificationChannels,
                               NotificationChannelListLeadTime joinNowChannels)
        {
            Debug.Assert(notificationChannels != null, "notificationChannels");
            Debug.Assert(joinNowChannels != null, "joinNowChannels");

            notificationChannelControl.Initialize(notificationChannels.Channels);

            _absoluteTime = notificationChannels.AbsoluteTime;
            if (PropertyChanged != null) // Notify WPF to read property AbsoluteTimeIndex.
            {
                PropertyChanged(this, new PropertyChangedEventArgs("AbsoluteTimeIndex"));
            }

            // Initialize channels and absolute time for join now.
            joinNowChannelControl.Initialize(joinNowChannels.Channels);
            
            _leadTime = joinNowChannels.LeadTime;
            if (PropertyChanged != null) // Notify WPF to read property LeadTimeIndex.
            {
                PropertyChanged(this, new PropertyChangedEventArgs("LeadTimeIndex"));
            }
        }

        /// <summary>
        /// Absolute time index for the notification channels time combo.
        /// </summary>
        public int AbsoluteTimeIndex
        {
            get
            {
                return _absoluteTime.Ticks == 0 ? 
                    0 :  
                    _absoluteTime.Hour * 60 / AbsoluteTimeIntervalInMinutes + _absoluteTime.Minute / AbsoluteTimeIntervalInMinutes + 1;
            }
            set
            {
                _absoluteTime = value == 0 ?
                    new DateTime() :
                    new DateTime(9999, 12, 31, 0, 0, 0).AddMinutes((value -1)* AbsoluteTimeIntervalInMinutes);
            }
        }

        /// <summary>
        /// Lead time index for the join now channels time combo.
        /// </summary>
        public int LeadTimeIndex
        {
            get
            {
                return _leadTime == -1 ? 0 : _leadTime / LeadTimeIntervalInMinutes + 1;
            }
            set
            {
                _leadTime = value == 0 ? -1 : (value - 1) * LeadTimeIntervalInMinutes;
            }
        }

        /// <summary>
        /// The list of notification channels and absolute time.
        /// </summary>
        public NotificationChannelListAbsoluteTime NotificationChannels
        {
            get
            {
                return new NotificationChannelListAbsoluteTime(notificationChannelControl.Channels.Channels, _absoluteTime);
            }
        }

        /// <summary>
        /// The list of join now channels and lead time.
        /// </summary>
        public NotificationChannelListLeadTime JoinNowChannels
        {
            get
            {
                return new NotificationChannelListLeadTime(joinNowChannelControl.Channels.Channels, _leadTime);
            }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Property changed event.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
